package com.lx.controller;

import com.lx.bean.*;
import com.lx.mvc.ResponseBody;
import com.lx.service.CourierService;
import com.lx.service.ExpressService;
import com.lx.service.UserService;
import com.lx.util.JsonUtil;
import com.lx.util.RandomUtil;
import com.lx.util.SMSUtil;
import com.lx.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description:
 * @Date Created in  2021-07-09 20:46
 * @Modified By:
 * @Author: 零巽
 */
public class UserController {

    @ResponseBody("/user/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        //获取前端信息
        String userName = req.getParameter("userName");
        String phone = req.getParameter("phone");
        String idNumber = req.getParameter("idNumber");
        String password = req.getParameter("password");
        //封装为对象
        User user = new User(userName, phone, idNumber, password);
        //调用Service方法
        boolean b = UserService.addUser(user);
        //根据结果封装信息
        Message message = new Message();
        if (b) {
            message.setStatus(0);
            message.setResult("添加成功！");
        } else {
            message.setStatus(-1);
            message.setResult("电话号码已存在，添加失败！");
        }
        //封装为Json对象返回
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/user/findByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp){
        String inputPhone = req.getParameter("inputPhone");
        User user = UserService.findByPhone(inputPhone);
        Message message = new Message();
        if (user != null) {
            message.setStatus(0);
            message.setData(user);
        } else {
            message.setStatus(-1);
            message.setResult("查询失败，手机号码不存在！");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String inputPhone = req.getParameter("inputPhone");
        boolean b = UserService.deleteByPhone(inputPhone);
        Message message = new Message();
        if (b) {
            message.setStatus(0);
            message.setResult("删除成功!");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败！");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String userName = req.getParameter("userName");
        String phone = req.getParameter("phone");
        String idNumber = req.getParameter("idNumber");
        String password = req.getParameter("password");
        User user = new User(userName, phone, idNumber, password);
        boolean update = UserService.update(Integer.parseInt(id), user);
        Message message = new Message();
        if (update) {
            message.setStatus(0);
            message.setResult("更新成功！");
        } else {
            message.setStatus(-1);
            message.setResult("更新失败，电话号码已存在！");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/user/list.do")
    public String getUserList(HttpServletRequest req, HttpServletResponse resp){
        String offset = req.getParameter("offset");
        String pageNumber = req.getParameter("pageNumber");
        List<BootstrapTableUser> allUser = UserService.getAllUser(true, Integer.parseInt(offset), Integer.parseInt(pageNumber));
        ResultData<BootstrapTableUser> resultData = new ResultData<>();
        resultData.setRows(allUser);
        int[] console = UserService.console();
        resultData.setTotal(console[0]);
        return JsonUtil.toJSON(resultData);
    }

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        int[] console = UserService.console();
        Message message = new Message();
        if (console != null) {
            message.setStatus(0);
            message.setData(console);
        } else {
            message.setStatus(-1);
        }
        return JsonUtil.toJSON(message);

    }

}
